/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kmeansclustering;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author hoangtam
 */
public class VisualLize extends JFrame {

    public void init(ArrayList<ArrayList<Integer>> data) {

        JFrame jf = new JFrame();
        jf.setTitle("VISUAL LIZE");
        jf.setSize(650, 550);
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
        //jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DataNormal f = new DataNormal();
        Vector points = f.readData();
        Panel p = new Panel();
        p.points = points;
        p.data = data;
        jf.add(p);

        p.addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                PanelMouseMoved(evt);
            }

            private void PanelMouseMoved(MouseEvent e) {

//               for (int i = 0; i < points.size(); i++) {
//                    Point p = (Point) points.get(i);
//                    if (e.getX() == (30+p.x) && e.getY() == (480-p.y)) 
//                        System.out.println("(" + p.x + ";" + p.y + ")");
//                    break;
//                }
            }
        });

    }

    public static void main(String[] args) {
        VisualLize in = new VisualLize();
        ArrayList<Integer> a1 = new ArrayList();
        ArrayList<Integer> a2 = new ArrayList();
        ArrayList<Integer> a3 = new ArrayList();
        for (int i = 0; i < 30; i++) {
            a1.add(i);
        }
        for (int i = 30; i < 60; i++) {
            a2.add(i);
        }
        for (int i = 60; i < 100; i++) {
            a3.add(i);
        }
        ArrayList<ArrayList<Integer>> data = new ArrayList<>();
        data.add(a1);
        data.add(a2);
        data.add(a3);
        in.init(data);
    }
}

class Panel extends JPanel {

    //DataNormal f;
    Vector points;
    ArrayList<ArrayList<Integer>> data;
//    DataNormal f = new DataNormal();
//    Vector points= f.readData();

    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.blue);

        g2d.drawLine(30, 30, 30, 480);
        g2d.drawLine(30, 30, 25, 35);
        g2d.drawLine(30, 30, 35, 35);
        g2d.drawLine(30, 480, 580, 480);
        g2d.drawString("x", 20, 30);
        g2d.drawString("y", 580, 500);
        g2d.drawLine(575, 475, 580, 480);
        g2d.drawLine(575, 485, 580, 480);
        g2d.drawLine(28, 280, 32, 280);
        g2d.drawLine(28, 80, 32, 80);
        g2d.drawLine(28, 180, 32, 180);
        g2d.drawLine(28, 380, 32, 380);
        for (int i = 1; i <= 4; i++) {
            g2d.drawLine((30 + i * 100), 482, (30 + i * 100), 478);

        }
        g2d.drawString("0", 25, 490);
        for (int i = 0; i < data.size(); i++) {
            int avg_x = 0;
            int avg_y = 0;
            int sum_x = 0;
            int sum_y = 0;
            int siz = data.get(i).size();
            for (int j = 0; j < siz; j++) {
                if (i == 0) {
                    PointNormal p = (PointNormal) points.get(data.get(i).get(j));
                    this.paintPoint(g, 30 + p.x, 480 - p.y, Color.red);
                    sum_x += p.x;
                    sum_y += p.y;

                }
                if (i == 1) {
                    PointNormal p = (PointNormal) points.get(data.get(i).get(j));
                    this.paintPoint(g, 30 + p.x, 480 - p.y, Color.green);
                    sum_x += p.x;
                    sum_y += p.y;

                }
                if (i == 2) {
                    PointNormal p = (PointNormal) points.get(data.get(i).get(j));
                    this.paintPoint(g, 30 + p.x, 480 - p.y, Color.BLACK);
                    sum_x += p.x;
                    sum_y += p.y;

                }

            }
            System.out.println("sumX=" + sum_x);
            System.out.println("sumY=" + sum_y);
            avg_x = sum_x / siz;
            avg_y = sum_y / siz;
            this.paintPointCentroid(g, 30 + avg_x, 480 - avg_y, Color.BLUE);
        }

//        for (int i = 0; i < points.size(); i++) {
//            PointNormal p = (PointNormal) points.get(i);
//            this.paintPoint(g, 30 + p.x, 480 - p.y, p.color);
//        }
    }

    public void paintPoint(Graphics g, int x, int y, Color color) {
//        Graphics2D g2d = (Graphics2D) g;
//        g2d.setColor(color);
//        g2d.drawOval(x, y, 4, 4);
        Rectangle rect = new Rectangle(30, 30, 30, 30);
        Mypolygon polygon = new Mypolygon(rect);
        
        g.setColor(color);
        g.drawPolygon(polygon);
        g.fillPolygon(polygon);
        //g.fillOval(x, y, 8, 8);
    }

    public void paintPointCentroid(Graphics g, int x, int y, Color color) {
//        Graphics2D g2d = (Graphics2D) g;
//        g2d.setColor(color);
//        g2d.drawOval(x, y, 4, 4);
        g.setColor(color);
        g.fillOval(x, y, 10, 10);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }
}

class Mypolygon extends Polygon implements ActionListener
{

    Rectangle rect;
    
    public Mypolygon(Rectangle rect)
    {
        this.rect = rect;
    }
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JOptionPane.showMessageDialog(null, "agudfgjdhfrgd");
        throw new UnsupportedOperationException("Not supported yet.");
    }
}